package l2hc.gameserver.network.serverpackets;

import l2hc.gameserver.Config;
import l2hc.gameserver.model.Creature;
import l2hc.gameserver.model.Player;
import l2hc.gameserver.network.serverpackets.components.SystemMsg;
import l2hc.gameserver.utils.AutoHuntingPunish;
import l2hc.gameserver.utils.Location;
import l2hc.gameserver.utils.Log;

public class CharMoveToLocation extends L2GameServerPacket
{
	private int _objectId, _client_z_shift;
	private Location _current;
	private Location _destination;
	private Creature _cha;

	public CharMoveToLocation(Creature cha)
	{
		_cha = cha;
		_objectId = cha.getObjectId();
		_current = cha.getLoc();
		_destination = cha.getDestination();
		if(!cha.isFlying())
			_client_z_shift = Config.CLIENT_Z_SHIFT;
		if(cha.isInWater())
			_client_z_shift += Config.CLIENT_Z_SHIFT;

		if(_destination == null)
		{
			Log.debug("CharMoveToLocation: desc is null, but moving. L2Character: " + cha.getObjectId() + ":" + cha.getName() + "; Loc: " + _current);
			_destination = _current;
		}
	}

	public CharMoveToLocation(int objectId, Location from, Location to)
	{
		_objectId = objectId;
		_current = from;
		_destination = to;
	}

	@Override
	protected final void writeImpl()
	{
		// Bot punishment restriction
		if(_cha instanceof Player && Config.ENABLE_AUTO_HUNTING_REPORT)
		{
			Player actor = (Player) _cha;
			if(actor.isBeingPunished())
			{
				if(actor.getPlayerPunish().canWalk() && actor.getPlayerPunish().getBotPunishType() == AutoHuntingPunish.Punish.MOVEBAN)
				{
					actor.endPunishment();
				}
				else
				{
					actor.sendPacket(new SystemMessage2(SystemMsg.YOU_HAVE_BEEN_REPORTED_AS_AN_ILLEGAL_PROGRAM_USER_SO_MOVEMENT_IS_PROHIBITED_FOR_120_MINUTES));
					return;
				}
			}
		}
		
		writeC(0x2f);

		writeD(_objectId);

		writeD(_destination.x);
		writeD(_destination.y);
		writeD(_destination.z + _client_z_shift);

		writeD(_current.x);
		writeD(_current.y);
		writeD(_current.z + _client_z_shift);
	}
}